package com.sys.console.cookie;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.sys.console.pojo.Sys_User;

/**
 * web常用方法的封装
 */
public class WebUtils {

	/**
	 * 检测是否已登录用户
	 */
	public static boolean isLoggedOn(HttpServletRequest request) {
		Object obj = getSession(request).getAttribute(Constant_Cookie.CURRENT_USER_SESSION);
		return (obj != null);
	}

	/**
	 * 获取当前的登录用户
	 */
	public static Sys_User getLoggedOnUser(HttpServletRequest request) {
		return (Sys_User) getSession(request).getAttribute(Constant_Cookie.CURRENT_USER_SESSION);
	}
	
	/**
	 * 获取当前的登录用户
	 */
	public static Sys_User getWebLoggedOnUser(HttpServletRequest request) {
		return (Sys_User) getSession(request).getAttribute(Constant_Cookie.SESSION_WEB_LOGIN_USER);
	}

	/**
	 * 在当前Session中登录用户
	 */
	public static void login(Sys_User user,HttpServletRequest request) {
		getSession(request).setAttribute(Constant_Cookie.CURRENT_USER_SESSION, user);
	}
	
	/**
	 * 在当前Session中登录用户
	 */
	public static void weblogin(Sys_User user,HttpServletRequest request) {
		getSession(request).setAttribute(Constant_Cookie.SESSION_WEB_LOGIN_USER, user);
	}

	/**
	 * 注销，移除当前Session中的登录信息
	 * 
	 * @param request
	 */
	public static void logout(HttpServletRequest request) {	
		getSession(request).removeAttribute(Constant_Cookie.CURRENT_USER_SESSION);
	}
	
	public static void web_logout(HttpServletRequest request) {	
		getSession(request).removeAttribute(Constant_Cookie.SESSION_WEB_LOGIN_USER);
	}

	/**
	 * 得到请求的uri
	 */
	public static String getRequestURI(HttpServletRequest request) {
		return request.getRequestURI();
	}

	/**
	 * 获得当前登录用户的sessionId
	 */
	public static String getSessionId(HttpServletRequest request) {
		return getSession(request).getId();
	}

	/**
	 * 获取上下文环境中的session
	 */
	public static HttpSession getSession(HttpServletRequest request) {
		return request.getSession();
	}

}
